// PyCVGL
// Copyright (c) 2012, Luke Williams
//
// Contact: pycvgl@shmookey.net
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
// 02111-1307  USA

#include <opencv/cv.h>
#include <gl/gl.h>

// Boost
#include <boost/python.hpp>

// Namespaces
using namespace boost::python;
using namespace cv;

// Lifted from cv2.cv.hpp (cv's python bindings)
struct cvmat_t {
  PyObject_HEAD
  CvMat *a;
  PyObject *data;
  size_t offset;
};

struct CVGLImage {
	CVGLImage (PyObject* imgObj) {
		image = (cvmat_t*) imgObj;
	};
	cvmat_t* image;
	int Height() { return image->a->rows; }
	int Width() { return image->a->cols; }
	void LoadRGBTexture (int textureID) {
		glBindTexture (GL_TEXTURE_2D, textureID);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->a->cols, image->a->rows, 0, GL_RGB, GL_UNSIGNED_BYTE, (void*) image->a->data.db);
	}
	void LoadRGBATexture (int textureID) {
		glBindTexture (GL_TEXTURE_2D, textureID);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->a->cols, image->a->rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*) image->a->data.db);
	}
};

BOOST_PYTHON_MODULE(CVGLImage)
{
	object cvmat_class (class_<cvmat_t> ("cv2.cv.cvmat"));
	class_<CVGLImage>("CVGLImage", init<PyObject*>())
		.def ("Height", &CVGLImage::Height)
		.def ("Width", &CVGLImage::Width)
		.def ("LoadRGBTexture", &CVGLImage::LoadRGBTexture)
		.def ("LoadRGBATexture", &CVGLImage::LoadRGBATexture);
}
